package com.jyx.thread;

// 多线程实现方法一：继承Thread类
class ThreadTicket extends Thread{
	// 票数不统一，会出现多于实际数量的票，不建议票数变成静态的
	private int ticket = 100;
	
	public void run(){
		while(true){
			if(ticket > 0){
				System.out.println(Thread.currentThread().getName() + "ticket : " + ticket);
			}
		}
	}
}

// 实现多线程的方法二： 实现Runnable借口，并实现空参run方法
class RunnableTicket implements Runnable{

	// 票的总数量
	private int ticketNum = 100;
	
	@Override
	public void run() {
		while(true){
			if(ticketNum > 0){
				System.out.println(Thread.currentThread().getName() + "ticket : " + ticketNum--);
			}
		}
	}
	
}

public class ThreadDemo {

	
	public static void main(String args[]){
		// 方法一：票数量不统一，但不建议使用静态
		/*ThreadTicket t1 = new ThreadTicket();
		ThreadTicket t2 = new ThreadTicket();
		ThreadTicket t3 = new ThreadTicket();
		ThreadTicket t4 = new ThreadTicket();
		t1.start();
		t2.start();
		t3.start();
		t4.start();*/
		// 方法二：让类实现Runnable接口
		// 创建空线程 Thread t = new Thread();
		// 让对象于线程建立关系：Thread t = new Thread(new RunnabletTicket());
		RunnableTicket t = new RunnableTicket();
		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		Thread t3 = new Thread(t);
		Thread t4 = new Thread(t);
		// 启动
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		
		
	}
	
	
	
}
